﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace modBusTest
{

    /// <summary>
    /// Simple struct to hold a base register value and a range to read from
    /// </summary>
    struct pair
    {
        public int baseRegister;
        public int range;
    }

    class optimizeRange
    {
    
        bool[] range;
        List<pair> solutionTable = new List<pair>();
        /// <summary>
        /// constructor that creates a table with number of slots specified by size
        /// </summary>
        /// <param name="size">number of elements in the table</param>
        public optimizeRange(int size)
        {   
            range = new bool[size];
            InitializeTable();
        }

        /// <summary>
        /// Default constructor makes a table of 10001 elements
        /// Really 10000, but 10001 to since plc starts with 1
        /// </summary>
        public optimizeRange()
        {
            range = new bool[100001];
            InitializeTable();
        }
        /// <summary>
        /// Internal function to make initialize table to have 
        /// no data elements
        /// </summary>
        private void InitializeTable()
        {
            for (int i = 0; i < range.Length; i++)
            {
                range[i] = false;
            }
        }

        /// <summary>
        /// Populates the table with registers that are used
        /// </summary>
        /// <param name="register"></param>
        public void addRegister(ushort register){
            range[register] = true;
        }


        /// <summary>
        /// Solve the for the minimum number of modbus commands
        /// </summary>
        public void solve()
        {
            for (int i = 0; i < range.Length; i++)
            {
                // this will look for parts in the table where value is true
                if (range[i])
                {
                    pair value = new pair(); // this will start tracking if the table is true
                    value.baseRegister = i;
                    value.range = 0;
                    while (range[i]  && value.range < 124)
                    {
                        value.range++;
                        i++;
                    }
                    // this is to make up for the fact that this will messup the count
                    //for example if we need to read 0-250 then when it increments to to 125 
                    // it will go to 126 which will then increment it to 127
                    if (value.range == 124)
                    {
                        i--;
                        //value.range--;
                    }


                    solutionTable.Add(value);
                }
            }
        }

        /// <summary>
        /// Gets the solution must run solve() before this command.
        /// </summary>
        /// <returns></returns>
        public List<pair> getSolution()
        {
            return solutionTable;
        }

        
    }
}
